<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="物料编码:" prop="materialDataId">
            <el-select v-model="formModel.materialDataId" clearable filterable>
              <el-option
                v-for="item in materialDataOptions"
                :key="item.materialDataId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.materialDataId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="追溯工位:" prop="tmBasUlocId">
            <el-select v-model="formModel.tmBasUlocId" clearable filterable>
              <el-option
                v-for="item in ulocOptions"
                :key="item.tmBasUlocId"
                :label="item.ulocNo+'-'+item.ulocName"
                :value="item.tmBasUlocId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置代码:" prop="vehicleConfigNoId">
            <el-select
              v-model="formModel.vehicleConfigNoId"
              clearable
              filterable
              @clear="clearConfigNo"
              @change="changeConfigNo"
            >
              <el-option
                v-for="item in configNoOptions"
                :key="item.vehicleConfigNoId"
                :label="item.configNo+'-'+item.configDesc"
                :value="item.vehicleConfigNoId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配置项代码:" prop="vehicleConfigItemNoId">
            <el-select v-model="formModel.vehicleConfigItemNoId" clearable filterable>
              <el-option
                v-for="item in configItemNoOptions"
                :key="item.vehicleConfigItemNoId"
                :label="item.configItemNo+'-'+item.partMaterialDesc"
                :value="item.vehicleConfigItemNoId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'VehicleConfigOptionalMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      materialDataOptions: [],
      configNoOptions: [],
      configItemNoOptions: [],
      ulocOptions: [],
      disabledNo: false,
      formModel: {
        vehicleConfigOptionalId: '',
        materialDataId: '',
        tmBasUlocId: '',
        vehicleConfigNoId: '',
        vehicleConfigItemNoId: ''
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      const param = { status: '1' }
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
        this.getMaterialDataOptions(param)
        this.getUlocList(param)
        this.getConfigNoOptions(param)
        param.vehicleConfigNoId = this.formModel.vehicleConfigNoId
        this.getConfigItemNoOptions(param)
      } else {
        this.getUlocList(param)
        this.getMaterialDataOptions(param)
        this.getConfigNoOptions(param)
      }
      this.initRules()
    },
    // 查询物料主数据列表
    getMaterialDataOptions(param) {
      this.get('/materialData/listMaterialData', param).then(res => {
        this.materialDataOptions = res
      })
    },
    /** 查询追溯工位列表 */
    getUlocList(param) {
      this.get('/tmBasUloc/queryTmBasUloc', param).then(res => {
        this.ulocOptions = res.list
      })
    },
    // 点击后查询配置项代码
    changeConfigNo(value) {
      if (value !== '') {
        const param = { vehicleConfigNoId: value }
        this.formModel.vehicleConfigItemNoId = ''
        this.getConfigItemNoOptions(param)
      }
    },
    // 查询配置代码
    getConfigNoOptions(param) {
      this.get('/vehicleConfigNo/listVehicleConfigNo', param).then(res => {
        this.configNoOptions = res
      })
    },
    // 清空配置代码
    clearConfigNo() {
      // 清空配置项代码及列表
      this.formModel.vehicleConfigItemNoId = ''
      this.configItemNoOptions = ''
    },
    // 查询配置项代码
    getConfigItemNoOptions(param) {
      this.get('/vehicleConfigItemNo/listVehicleConfigItemNo', param).then(res => {
        this.configItemNoOptions = res
      })
    },

    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        materialDataId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        tmBasUlocId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vehicleConfigNoId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        vehicleConfigItemNoId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.vehicleConfigOptionalId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/vehicleConfigOptional/update'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/vehicleConfigOptional/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
